package com.sskj.run.ui.fragment;


import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hey.lib.HeySpinner;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.bean.CoinTypeBean;
import com.sskj.run.bean.RankBean;
import com.sskj.run.presenter.RankFragmentPresenter;
import com.sskj.run.util.HideUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.RUN_FRAGMENT_RANK)//排行榜
public class RankFragment extends BaseFragment<RankFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.spinner)
    HeySpinner spinner;
    @BindView(R2.id.tv_rank_type)
    TextView tv_rank_type;
    @BindView(R2.id.spinner_type)
    HeySpinner spinner_type;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private ModelRx2DataSource<RankBean> dataSource;
    private MVCCoolHelper<List<RankBean>> mvcHelper;
    private int name = 0;
    private BottomSheetDialog bottomSheetName;
    private BottomSheetDialog bottomSheetType;
    private List<String> payTypes;
    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.run_fragment_rank;
    }

    @Override
    public RankFragmentPresenter getPresenter() {
        return new RankFragmentPresenter();
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.run_recy_item_rank, new SlimInjector<RankBean>() {
            @Override
            public void onInject(RankBean data, IViewInjector injector, List payloads) {
                String palyerName = data.getRealname();
                if (HideUtil.isInteger(palyerName)) {
                    if (palyerName.length() == 11) {
                        palyerName = HideUtil.getPhoneHide(palyerName);
                    }
                }
//                if (TextUtils.isEmpty(data.getUpic())) {
//                    injector.image(R.id.iv_rank_header, App.INSTANCE.getResources().getDrawable(R.mipmap.run_icon_wolf));
//                } else {
//                    injector.imageCircle(R.id.iv_rank_header, BaseHttpConfig.BASE_URL + data.getUpic());
//                }
                injector.visibility(R.id.iv_item_rank, slimAdapter.getData().indexOf(data) < 3 ? View.VISIBLE : View.GONE)
                        .visibility(R.id.tv_item_rank, slimAdapter.getData().indexOf(data) > 2 ? View.VISIBLE : View.GONE)
                        .text(R.id.tv_item_rank, slimAdapter.getData().indexOf(data) + 1 + "")
                        .text(R.id.tv_rank_name, palyerName)
                        .text(R.id.tv_bet_total_money, name == 0 ? data.getZuigao() : data.getLeiji())
                        .background(R.id.iv_item_rank, String.valueOf(slimAdapter.getData().indexOf(data)).equals("0") ?
                                App.INSTANCE.getResources().getDrawable(R.mipmap.run_icon_cup_1)
                                : String.valueOf(slimAdapter.getData().indexOf(data)).equals("1") ?
                                App.INSTANCE.getResources().getDrawable(R.mipmap.run_icon_cup_2)
                                : App.INSTANCE.getResources().getDrawable(R.mipmap.run_icon_cup_3));

            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        dataSource = new ModelRx2DataSource<RankBean>(
                (ModelRx2DataSource.OnLoadSource<RankBean>) page -> mPresenter.getRankList(
                        spinner_type.getText().toString(),
                        spinner.getSelectIndex() == 0 ? "zuigao" : "leiji"
                        , page, 10), 10) {
            @Override
            public boolean hasMore() {
                return false;
            }
        };
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.setAutoLoadMore(false);
    }

    @Override
    protected void initData() {
        mPresenter.getCoinType();
    }

    public void initSpinner(List<CoinTypeBean> coinTypeBeans) {
        Flowable.fromIterable(coinTypeBeans)
                .filter(coinTypeBean -> coinTypeBean.getIs_game() == 1)
                .map(coinTypeBean -> coinTypeBean.getCode())
                .toList()
                .subscribe((strings, throwable) -> {
                    List<String> payNames = Arrays.asList(getResources().getStringArray(R.array.run_rank_names));

                    payTypes = strings;
                    spinner.setText(payNames.get(0));
                    spinner_type.setText(payTypes.get(0));
                    updataList();
                    ClickUtil.click(spinner, () -> {
                        if (bottomSheetName == null) {
                            bottomSheetName = BottomSheetUtil.getBottomSheet(getActivity(), null, (recyclerView1, i, view) -> {
                                bottomSheetName.dismiss();
                                name = i;
                                tv_rank_type.setText(name == 0 ? App.INSTANCE.getString(R.string.run_rankFragment1) : App.INSTANCE.getString(R.string.run_rankFragment2));
                                spinner.setText(name == 0 ? App.INSTANCE.getString(R.string.run_rankFragment1) : App.INSTANCE.getString(R.string.run_rankFragment2));
                                updataList();
                            }, payNames);
                        }
                        bottomSheetName.show();
                    });
                    ClickUtil.click(spinner_type, () -> {
                        if (bottomSheetType == null) {
                            bottomSheetType = BottomSheetUtil.getBottomSheet(getActivity(), null, (recyclerView1, i, view) -> {
                                bottomSheetType.dismiss();
                                type = payTypes.get(i);
                                spinner_type.setText(type);
                                updataList();
                            }, payTypes);
                        }
                        bottomSheetType.show();
                    });

                });

    }

    public void updataList() {

        mvcHelper.refresh();
    }
}
